package net.xiaoyu233.mitemod.miteite.gui;

import net.minecraft.*;
import net.xiaoyu233.mitemod.miteite.inventory.container.ContainerRecyclingTable;
import net.xiaoyu233.mitemod.miteite.tileentity.TileEntityRecyclingTable;
import org.lwjgl.opengl.GL11;

import java.util.List;

public class GuiRecyclingTable extends awy  implements ICrafting{
    private static final bjo guiTexture = new bjo("textures/gui/container/recycling_table.png");
    private TileEntityRecyclingTable recyclingTable;
    private final EntityPlayer player;

    public GuiRecyclingTable(EntityPlayer player, TileEntityRecyclingTable tileEntity) {
        super(new ContainerRecyclingTable(player, tileEntity));
        this.recyclingTable = tileEntity;
        this.player = player;
        this.e.onCraftGuiOpened(this);
    }

    @Override
    protected void b(int mouseX, int mouseY) {
        String title =  this.recyclingTable.hasCustomName() ? this.recyclingTable.getCustomNameOrUnlocalized() : bkb.a(this.recyclingTable.getCustomNameOrUnlocalized());

        this.o.b(title , this.c / 2 - this.o.a(title) / 2 + 5, 6, 4210752);
        this.o.b(bkb.a("container.inventory"), 8, this.d - 96 + 2, 4210752);

        // 显示回收台等级
        String levelText = bkb.a("等级: " + recyclingTable.getTableLevelName());
        this.o.b(levelText, this.c - this.o.a(levelText) - 5, 6, 4210752);

        // 显示物品耐久度信息
        for (int i = 0; i < 2; i++) {
            if (recyclingTable.getStackInSlot(i) != null) {
                String durabilityInfo = recyclingTable.getDurabilityInfo(i);
                this.o.b(durabilityInfo, this.c / 2 - this.o.a(durabilityInfo) / 2 - 120, 6 + (i*10), 0xffffff);
            }
        }

        // 显示回收进度
        int progress =  recyclingTable.getRecyclingProgress();
        int totalTime = recyclingTable.getCurrentRecyclingTime();

        if (progress > 0) {
            int secondsRemaining = (totalTime - progress) / 20;
            String progressText = bkb.a("回收中: " + secondsRemaining + "秒");
            this.o.b( progressText, this.c / 2 - this.o.a(progressText) / 2+120, 6,0xffffff);
        }

        // 检查是否有岩浆
        if (!recyclingTable.hasLava()) {
            String lavaWarning = bkb.a("需要下方有岩浆!");
            this.o.b(lavaWarning, this.c / 2 - this.o.a(lavaWarning)/2, 72, 0xFF0000);
        }
    }

    @Override
    protected void a(float partialTicks, int mouseX, int mouseY) {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.f.J().a(guiTexture);
        int x = (this.g - this.c) / 2;
        int y = (this.h - this.d) / 2;
        this.b(x, y, 0, 0, this.c, this.d);

        // 绘制进度条
        int progress = recyclingTable.getRecyclingProgress();
        int totalTime = recyclingTable.getCurrentRecyclingTime();
        boolean haslava = recyclingTable.hasLava();
        if (progress > 0  && totalTime > 0) {
            int progressWidth = (int) (24.0F * progress / totalTime);
            this.b(x+82, y+35 , 176, 14, progressWidth, 16);
        }
        if (haslava){
            this.b(x+85, y+55 , 176, 0, 14, 14);
        }

        // 根据回收台等级绘制不同的图标
//        int level = recyclingTable.getTableLevel();
//        if (level > 0) {
//            int iconY = 30 + (level * 2); // 根据等级调整图标位置
//            this.b(x+150, y+5, 200, iconY, 16, 16); // 假设在纹理中有不同等级的图标
//        }
    }

    @Override
    public void updateCraftingInventory(Container container, List list) {

    }

    @Override
    public void sendSlotContents(Container container, int i, ItemStack itemStack) {

    }

    @Override
    public void sendProgressBarUpdate(Container container, int id, int value) {
        if (id == 0) {
            recyclingTable.setRecyclingProgress(value);
        } else if (id == 1) {
            recyclingTable.setCurrentRecyclingTime(value);
        } else if (id == 2) {
            recyclingTable.setHasLava(value == 1);
        } else if (id == 3) {
            recyclingTable.setTableLevel(value);
        }
    }

    @Override//onGuiClosed
    public void b() {
        super.b();
        this.e.b(this);
    }
}